class PullReq
  attr_accessor :github, :reviewcomments

  def initialize(github)
    @github = github
    @reviewcomments = PullReviewComments.new(github)
  end

  def listPullRequests(repo, state=nil, user=nil)
    username = user == nil ? @github.username : user
    url = 'repos/%s/%s/pulls' % [username, repo]
    url = '%s?%s' % state ? [url, @github.parameterize(state)] : url
    @github.get(url)
  end

  def getPullRequest(repo, id, user=nil)
    username = user == nil ? @github.username : user
    @github.get(
        'repos/%s/%s/pulls/%s' % [username, repo, id])
  end

  def createPullRequest(repo, title, base, head, body=nil, user=nil)
    username = user == nil ? @github.username : user
    params = {
        :title => title,
        :base => base,
        :head => head,
        :body => body
    }
    params = @github.removeEmptyParams(params)
    data = params.to_json
    @github.post('repos/%s/%s/pulls' % [username, repo], data)
  end

  def createPullRequestFromIssue(repo, issue, base, head, user=nil)
    username = user == nil ? @github.username : user
    params = {
        :issue => issue,
        :base => base,
        :head => head
    }
    data = params.to_json
    @github.post('repos/%s/%s/pulls' % [username, repo], data)
  end

  def listPullRequestCommits(repo, id, user=nil)
    username = user == nil ? @github.username : user
    @github.get(
        'repos/%s/%s/pulls/%s/commits' % [username, repo, id])
  end

  def listPullRequestFiles(repo, id, user=nil)
    username = user == nil ? @github.username : user
    @github.get(
        'repos/%s/%s/pulls/%s/files' % [username, repo, id])
  end

  def getIfPullRequestMerged(repo, id, user=nil)
    username = user == nil ? @github.username : user
    @github.get(
        'repos/%s/%s/pulls/%s/merge' % [username, repo, id])
  end

  def mergePullRequest(repo, id, commit_message=nil, user=nil)
    username = user == nil ? @github.username : user
    if commit_message
      params = {
          :commit_message => commit_message
      }
      data = params.to_json
      @github.put('repos/%s/%s/pulls/%s/merge' % [username, repo, id], data)
    else
      @github.put('repos/%s/%s/pulls/%s/merge' % [username, repo, id])
    end
  end
end